package com.example.distillapplication.ui.fragment.log;

import android.os.Bundle;
import android.os.Parcel;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.example.distillapplication.R;
import com.example.distillapplication.data.entity.OperationLog;

import java.util.ArrayList;
import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    List<OperationLog> operationLogs = new ArrayList<>();
    IGotoDataFragment gotoDataFragment;


    public void setGotoDataFragment(IGotoDataFragment gotoDataFragment) {
        this.gotoDataFragment = gotoDataFragment;
    }

    public void setOperationLogs(List<OperationLog> operationLogs) {
        this.operationLogs = operationLogs;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View itemView = layoutInflater.inflate(R.layout.cell_card, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        final OperationLog operationLog = operationLogs.get(position);
        holder.number.setText(String.valueOf(operationLog.getOperationId()));
        holder.date.setText(operationLog.getLoginTime());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();        //得到bundle对象
                bundle.putInt("operationId", operationLog.getOperationId());
                bundle.putInt("userId", operationLog.getUserId());
                bundle.putString("loginTime", operationLog.getLoginTime());
                bundle.putString("logoutTime", operationLog.getLogoutTime());
                bundle.putString("operationData", operationLog.getOperationData());
                gotoDataFragment.goToDataFragment(bundle);
            }
        });
        holder.date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();        //得到bundle对象
                bundle.putInt("operationId", operationLog.getOperationId());
                bundle.putInt("userId", operationLog.getUserId());
                bundle.putString("loginTime", operationLog.getLoginTime());
                bundle.putString("logoutTime", operationLog.getLogoutTime());
                bundle.putString("operationData", operationLog.getOperationData());
                gotoDataFragment.goToDataFragment(bundle);
            }
        });
        holder.number.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();        //得到bundle对象
                bundle.putInt("operationId", operationLog.getOperationId());
                bundle.putInt("userId", operationLog.getUserId());
                bundle.putString("loginTime", operationLog.getLoginTime());
                bundle.putString("logoutTime", operationLog.getLogoutTime());
                bundle.putString("operationData", operationLog.getOperationData());
                gotoDataFragment.goToDataFragment(bundle);
            }
        });
        holder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();        //得到bundle对象
                bundle.putInt("operationId", operationLog.getOperationId());
                bundle.putInt("userId", operationLog.getUserId());
                bundle.putString("loginTime", operationLog.getLoginTime());
                bundle.putString("logoutTime", operationLog.getLogoutTime());
                bundle.putString("operationData", operationLog.getOperationData());
                gotoDataFragment.goToDataFragment(bundle);
            }
        });
    }

    @Override
    public int getItemCount() {
        return operationLogs.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder{
        TextView number, date;
        ConstraintLayout layout;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            number = itemView.findViewById(R.id.number);
            date = itemView.findViewById(R.id.date);
            layout = itemView.findViewById(R.id.layout);
        }
    }
}
